package basic;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: 海琳琦
 * @Date: 2021/8/12 20:24
 */
public class Title3 {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abba"));
    }

    private static int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> result = new HashMap<Character,Integer>();
        int start=0,max=0;
        for (int i = 0; i < s.length(); i++) {
            if(result.containsKey(s.charAt(i))){
                //防止start后退
                start = Math.max(start, result.get(s.charAt(i))+1);
            }
            //更新位置
            result.put(s.charAt(i),i);
            max = Math.max(max, i-start+1);
        }
        return max;
    }
}
